﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace HostsEditor
{
    public static class Console
    {
        private static bool ConsoleShowed = false;

        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();

        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetStdHandle(int nStdHandle);

        [DllImport("kernel32.dll")]
        public static extern bool SetConsoleTitle(String lpConsoleTitle);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, CharacterAttributes wAttributes);

        public enum CharacterAttributes
        {
            FOREGROUND_BLUE = 0x0001,
            FOREGROUND_GREEN = 0x0002,
            FOREGROUND_RED = 0x0004,
            FOREGROUND_INTENSITY = 0x0008,
            BACKGROUND_BLUE = 0x0010,
            BACKGROUND_GREEN = 0x0020,
            BACKGROUND_RED = 0x0040,
            BACKGROUND_INTENSITY = 0x0080,
            COMMON_LVB_LEADING_BYTE = 0x0100,
            COMMON_LVB_TRAILING_BYTE = 0x0200,
            COMMON_LVB_GRID_HORIZONTAL = 0x0400,
            COMMON_LVB_GRID_LVERTICAL = 0x0800,
            COMMON_LVB_GRID_RVERTICAL = 0x1000,
            COMMON_LVB_REVERSE_VIDEO = 0x4000,
            COMMON_LVB_UNDERSCORE = 0x8000
        }

        private static string[] arguments = null;
        public static string[] CommandArguments
        {
            get { return arguments; }
        }

        public static void Run(string[] args)
        {
            arguments = args;

            if (CommandArguments.Length > 0 && string.Compare(CommandArguments[0], "-console") == 0)
            {
                ConsoleShowed = true;
                ShowConsole();
            }
            else
            {
                string command = string.Empty;
                foreach (string s in CommandArguments)
                    command = command + " " + s;
                ProcessCommand(command);
            }
        }

        public static void ShowConsole()
        {
            AllocConsole();

            IntPtr hStdWin = GetStdHandle(-11);
            SetConsoleTitle("HostsEditor: Console mode");
            System.Console.Write(">");
            ProcessCommand(System.Console.ReadLine());

            FreeConsole();
        }

        public static void ProcessCommand(string command)
        {
            string[] commandParts = command.Split(':');

            if (commandParts.Length > 1)
            {
                switch (commandParts[0].Trim())
                {
                    case "-add":
                        Add(commandParts[1].Trim());
                        break;
                    default:
                        WriteLine("Unknow command");
                        break;
                }
                WaitToInput();
            }
            else
            {
                if (string.Compare("exit", commandParts[0].Trim()) == 0 || string.Compare("-exit", commandParts[0].Trim()) == 0)
                {
                    System.Windows.Forms.Application.Exit();
                }
                else
                {
                    WriteLine("Unknow command");
                    WaitToInput();
                }
            }            
        }

        public static void WaitToInput()
        {
            if (ConsoleShowed)
            {
                System.Console.WriteLine();
                System.Console.Write(">");
                ProcessCommand(System.Console.ReadLine());
            }
        }

        public static void Add(string arguments)
        {
            bool flag = false;
            HostCollection host = new HostCollection();
            host.Load();

            Host hostRecord = Host.Parse(arguments);

            if (!hostRecord.IsEmpty)
                host.Add(hostRecord);
            else
                WriteLine("Unknow IP/Host. Read ReadME.txt for more information about commands in console mode or use GUI mode.");

            try
            {
                flag = host.Save();
            }
            catch
            {
                WriteLine("Unknow error ouccured when saving file. File not saved.");
            }
            if (flag)
                WriteLine("New IP/Hostname combination added to list.");

        }

        private static void WriteLine(string line)
        {
            if (ConsoleShowed)
                System.Console.WriteLine(line);
        }
    }
}